package main;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import tool.JavaCodeAnalyzer;

/**
 *
 * @author linkai
 */
public class Main {

    public static void main(String[] args) {
        JavaCodeAnalyzer codeAnalyzer = new JavaCodeAnalyzer();
        Scanner in = new Scanner(System.in);
        File file;
        try {
            do {
                System.out.print("请输入要统计的文件名:");
                String filename = in.nextLine();
                file = new File(filename);
                if (!file.exists()) {
                    System.out.println("该目录不存在，请重新输入");
                }else if(!file.isDirectory()){
                    System.out.println("该路径不是一个目录");
                }
            } while (!file.exists()&&!file.isDirectory());
            codeAnalyzer.setPath(file.getAbsolutePath());
            codeAnalyzer.scannerFile(file, 0);
            codeAnalyzer.codeAnalyzer();
            String codePercent = String.format("%.2f", ((double) codeAnalyzer.getRowOfCode() / codeAnalyzer.getRowOfFile()) * 100);
            String notePercent = String.format("%.2f", ((double) codeAnalyzer.getRowOfNote() / codeAnalyzer.getRowOfFile()) * 100);
            String blankPercent = String.format("%.2f", ((double) codeAnalyzer.getRowOfBlank() / codeAnalyzer.getRowOfFile()) * 100);
            System.out.println("目录名称:" + file.getAbsolutePath());
            System.out.println("共有源文件:" + codeAnalyzer.getNumOfFile() + "个 , "
                    + "总的大小为:" + codeAnalyzer.getLengthOfFile() + "字节");
            System.out.println("源程序文件总行数:" + codeAnalyzer.getRowOfFile() + ",其中:");
            System.out.println("代码行数:" + codeAnalyzer.getRowOfCode() + ",占" +codePercent+"%" );
            System.out.println("注释行数:" + codeAnalyzer.getRowOfNote() + ",占" +notePercent+"%");
            System.out.println("空白行数:" + codeAnalyzer.getRowOfBlank() + ",占" +blankPercent+"%"+"\n");
            System.out.println("源程序中使用过的关键字有（按使用次数降序排列）:");
            System.out.println(codeAnalyzer.getKeyWord());
            SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
            System.out.println("本次分析的时间:"+time.format(new Date()));
            codeAnalyzer.writeRecordToFile();
        } catch (Exception ex) {
                System.out.println("程序错误，请重新运行");
        }
    }

}
